package com.klw.business.service.impl;

import java.util.List;

import com.klw.business.domain.KlmBatchModel;
import com.klw.business.domain.KlwWarningResults;
import com.klw.business.mapper.KlwWarningResultsMapper;
import com.klw.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.klw.business.mapper.KlwStudentsMapper;
import com.klw.business.domain.KlwStudents;
import com.klw.business.service.IKlwStudentsService;

/**
 * 学生Service业务层处理
 *
 * @author klw
 * @date 2025-08-24
 */
@Service
public class KlwStudentsServiceImpl implements IKlwStudentsService {
    @Autowired
    private KlwStudentsMapper klwStudentsMapper;

    @Autowired
    private MLPredictionServiceImpl mlPredictionService;

    @Autowired
    private KlwWarningResultsMapper klwWarningResultsMapper;

    /**
     * 查询学生
     *
     * @param studentId 学生主键
     * @return 学生
     */
    @Override
    public KlwStudents selectKlwStudentsByStudentId(Long studentId) {
        return klwStudentsMapper.selectKlwStudentsByStudentId(studentId);
    }

    /**
     * 查询学生列表
     *
     * @param klwStudents 学生
     * @return 学生
     */
    @Override
    public List<KlwStudents> selectKlwStudentsList(KlwStudents klwStudents) {
        return klwStudentsMapper.selectKlwStudentsList(klwStudents);
    }

    /**
     * 新增学生
     *
     * @param klwStudents 学生
     * @return 结果
     */
    @Override
    public int insertKlwStudents(KlwStudents klwStudents) {
        klwStudents.setCreateTime(DateUtils.getNowDate());
        return klwStudentsMapper.insertKlwStudents(klwStudents);
    }

    /**
     * 修改学生
     *
     * @param klwStudents 学生
     * @return 结果
     */
    @Override
    public int updateKlwStudents(KlwStudents klwStudents) {
        klwStudents.setUpdateTime(DateUtils.getNowDate());
        return klwStudentsMapper.updateKlwStudents(klwStudents);
    }

    /**
     * 批量删除学生
     *
     * @param studentIds 需要删除的学生主键
     * @return 结果
     */
    @Override
    public int deleteKlwStudentsByStudentIds(String[] studentIds) {
        return klwStudentsMapper.deleteKlwStudentsByStudentIds(studentIds);
    }

    /**
     * 删除学生信息
     *
     * @param studentId 学生主键
     * @return 结果
     */
    @Override
    public int deleteKlwStudentsByStudentId(Long studentId) {
        return klwStudentsMapper.deleteKlwStudentsByStudentId(studentId);
    }

    /**
     * 预测单个学生
     *
     * @param klmBatchModel 预测参数
     */
    @Override
    public void predictSingle(KlmBatchModel klmBatchModel) throws Exception {
        KlwWarningResults klwWarningResults = mlPredictionService.predictAndSave(klmBatchModel.getStudentId(), klmBatchModel.getSemesterId());
        KlwWarningResults exist = klwWarningResultsMapper.selectByStudentAndSemester(klmBatchModel.getStudentId(), klmBatchModel.getSemesterId());
        if (exist != null) {
            klwWarningResults.setWarningId(exist.getWarningId());
            klwWarningResultsMapper.updateKlwWarningResults(klwWarningResults);
        } else {
            klwWarningResultsMapper.insertKlwWarningResults(klwWarningResults);
        }
    }

    /**
     * 批量预测单个学生
     *
     * @param klmBatchModel 预测参数
     */
    @Override
    public void predicBatch(KlmBatchModel klmBatchModel) throws Exception {
        List<KlwWarningResults> klwWarningResults = mlPredictionService.batchPredictAndSave(klmBatchModel.getSemesterId());
        for (KlwWarningResults item : klwWarningResults){
            KlwWarningResults exist = klwWarningResultsMapper.selectByStudentAndSemester(item.getStudentId(), item.getSemesterId());
            if (exist != null) {
                item.setWarningId(exist.getWarningId());
                klwWarningResultsMapper.updateKlwWarningResults(item);
            }else{
                klwWarningResultsMapper.insertKlwWarningResults(item);
            }
        }
    }
}
